package com.jch.protobuf.client;

import com.jch.protobuf.domain.MsgBody;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

public class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        // Protobuf Varint 32 帧解码器 （128）
        pipeline.addLast(new ProtobufVarint32FrameDecoder());
        // Protobuf 解码器
        pipeline.addLast(new ProtobufDecoder(MsgBody.getDefaultInstance()));
        // Protobuf Varint 32 帧编码器（128）
        pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
        // Protobuf 编码器
        pipeline.addLast(new ProtobufEncoder());

        pipeline.addLast(new ClientHandler());
    }
}
